{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1200, 800, 3)\n",
      "(1200, 800)\n"
     ]
    }
   ],
   "source": [
    "## 1.图像的灰度处理\n",
    "## 方法一、\n",
    "import cv2 \n",
    "img1 = cv2.imread('../../imags/test.jpeg',1)\n",
    "img0 = cv2.imread('../../imags/test.jpeg',0)\n",
    "print(img1.shape)\n",
    "print(img0.shape)\n",
    "cv2.imshow('img0',img0)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 方法二\n",
    "import cv2 \n",
    "img1 = cv2.imread('../../imags/test.jpeg',1)\n",
    "dstimg = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)   ##颜色空间转换 1 data  2转换类型\n",
    "cv2.imshow('dst',dstimg)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 方法三 （b g r 相加取均值）\n",
    "import cv2\n",
    "import numpy as np\n",
    "img1 = cv2.imread('../../imags/test.jpeg',1)\n",
    "imgInfo = img1.shape\n",
    "height = imgInfo[0]\n",
    "width = imgInfo[1]\n",
    "dst = np.zeros(imgInfo,np.uint8)\n",
    "for i in range(0,height):\n",
    "    for j in range(0,width):\n",
    "        (b,g,r) = img1[i,j]\n",
    "        gray = (int(b)+int(g)+int(r))/3\n",
    "        dst[i,j] = np.uint8(gray)\n",
    "        #dst[i][j] = np.uint8(int(img1[i,j][0]) + int(img1[i,j][1]) + int(img1[i,j][2])/3)\n",
    "cv2.imshow('dst',dst)\n",
    "cv2.waitKey(0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 方法四  r*0.299 + g*0.587 + b*0.114\n",
    "import cv2\n",
    "import numpy as np\n",
    "img1 = cv2.imread('../../imags/test.jpeg',1)\n",
    "imgInfo = img1.shape\n",
    "height = imgInfo[0]\n",
    "width = imgInfo[1]\n",
    "dst = np.zeros(imgInfo,np.uint8)\n",
    "for i in range(0,height):\n",
    "    for j in range(0,width):\n",
    "        (b,g,r) = img1[i,j]\n",
    "        b = int(b)\n",
    "        g = int(g)\n",
    "        r = int(r)\n",
    "        gray = r*0.299 + g*0.587 + b*0.114\n",
    "        dst[i,j] = np.uint8(gray)\n",
    "cv2.imshow('dst',dst)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 灰度算法优化\n",
    "##1.定点 << 浮点\n",
    "##2.+- << */\n",
    "## 方法四  r*0.299 + g*0.587 + b*0.114\n",
    "import cv2\n",
    "import numpy as np\n",
    "img1 = cv2.imread('../../imags/test.jpeg',1)\n",
    "imgInfo = img1.shape\n",
    "height = imgInfo[0]\n",
    "width = imgInfo[1]\n",
    "dst = np.zeros(imgInfo,np.uint8)\n",
    "for i in range(0,height):\n",
    "    for j in range(0,width):\n",
    "        (b,g,r) = img1[i,j]\n",
    "        b = int(b)\n",
    "        g = int(g)\n",
    "        r = int(r)\n",
    "        ##gray = r*0.299 + g*0.587 + b*0.114\n",
    "        ##gray = (r*1+g*2+b*1)/4   ##浮点转定点，精度高可以提高倍数，*400\n",
    "        gray = (r+g<<1+b)>>2    ##乘除变移位\n",
    "        dst[i,j] = np.uint8(gray)\n",
    "cv2.imshow('dst',dst)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
